﻿using Bandit.Helpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ETC.DLEDLightingSystemPlus.Models
{
    public class Setting : Bandit.UI.MVVM.NotifyObject
    {
        public static string SavePath { get { return "".GetBaseDirectory() + "Setting.xml"; } }

        /** Software Setting **/

        #region Languages
        /// <summary>
        /// Languages
        /// </summary>
        [System.Xml.Serialization.XmlIgnore]
        public Dictionary<string, string> Languages { get; set; }

        private string _Language;
        /// <summary>
        /// Selected Language
        /// </summary>
        public string Language
        {
            get { return _Language; }
            set
            {
                _Language = value;

                OnPropertyChanged("Language");

                App.ChangeLanguage(value);

                LoadThemes();

                LoadPortNames();

                LoadConfigNames();

                //LoadtcgNames();
            }
        }
        #endregion

        #region Themes
        /// <summary>
        /// Languages
        /// </summary>
        [System.Xml.Serialization.XmlIgnore]
        public Dictionary<string, string> Themes { get; set; }

        private string _Theme;
        /// <summary>
        /// Selected Language
        /// </summary>
        public string Theme
        {
            get { return _Theme; }
            set
            {
                if (value != null)
                {
                    _Theme = value;

                    OnPropertyChanged("Theme");

                    App.ChangeTheme(value);
                }
            }
        }
        #endregion

        #region TestingHide
        private bool _TestingHide;
        /// <summary>
        /// 測試時是否隱藏
        /// </summary>
        public bool TestingHide
        {
            get { return _TestingHide; }
            set
            {
                _TestingHide = value;
                OnPropertyChanged("TestingHide");
            }
        }
        #endregion

        private bool _IsFailEnd = true;

        public bool IsFailEnd
        {
            get { return _IsFailEnd; }
            set
            {
                _IsFailEnd = value;
                OnPropertyChanged(nameof(IsFailEnd));
            }
        }

        #region IsSendHeartBeat 是否发送心跳包
        private bool _IsSendHeartBeat = false;

        public bool IsSendHeartBeat
        {
            get { return _IsSendHeartBeat; }
            set
            {
                _IsSendHeartBeat = value;
                OnPropertyChanged(nameof(IsSendHeartBeat));
            }
        }
        #endregion

        private int _MaxColumnCount = 2;
        /// <summary>
        /// 多工位界面显示的最大列数
        /// </summary>
        public int MaxColumnCount
        {
            get { return _MaxColumnCount; }
            set
            {
                _MaxColumnCount = value;
                OnPropertyChanged("MaxColumnCount");
            }
        }
        public Setting()
        {
            this.Languages = new Dictionary<string, string>();
            if (System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"Language\Language.xml"))
            {
                System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + @"Language\Language.xml");

                foreach (var item in doc.Root.Elements())
                {
                    if (item.Attribute("Key") != null && item.Attribute("Value") != null)
                    {
                        this.Languages.Add(item.Attribute("Key").Value, item.Attribute("Value").Value);
                    }
                }
            }
        }

        public void LoadThemes()
        {
            this.Themes = new Dictionary<string, string>();
            if (System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"Theme\Theme.xml"))
            {
                System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + @"Theme\Theme.xml");

                foreach (var item in doc.Root.Elements())
                {
                    if (item.Attribute("Key") != null)
                    {
                        this.Themes.Add(item.Attribute("Key").Value, App.GetString(item.Attribute("Key").Value));
                    }
                }
            }

            OnPropertyChanged("Themes");
            this.Theme = this.Theme;

            //var dict = new Dictionary<string, string>();
            //dict.Add("Light", App.GetString("Light"));
            //dict.Add("Dark", App.GetString("Dark"));

            //this.Themes = dict;

            //OnPropertyChanged("Themes");

            //this.Theme = this.Theme;
        }

        private Dictionary<string, string> _ConfigNames;

        [System.Xml.Serialization.XmlIgnore]
        public Dictionary<string, string> ConfigNames
        {
            get { return _ConfigNames; }
            set
            {
                _ConfigNames = value;
                OnPropertyChanged("ConfigNames");
            }
        }

        public void LoadConfigNames()
        {
            ConfigNames = new Dictionary<string, string>();
            DirectoryInfo di = new DirectoryInfo("".GetBaseDirectory() + "Configs\\");
            FileInfo[] files = di.GetFiles();
            foreach (FileInfo fi in files)
            {
                if (fi.Extension.Equals(".config") && fi.Name.ToLower().Contains("teststations"))
                {
                    ConfigNames.Add(fi.Name, fi.Name);
                }
            }
        }

        private string _tcgName;
        public string tcgName
        {
            get { return _tcgName; }
            set
            {
                _tcgName = value;
                OnPropertyChanged("tcgName");
            }
        }

        private Dictionary<string, string> _tcgNames;

        [System.Xml.Serialization.XmlIgnore]
        public Dictionary<string, string> tcgNames
        {
            get { return _tcgNames; }
            set
            {
                _tcgNames = value;
                OnPropertyChanged("tcgNames");
            }
        }

        public void LoadtcgNames()
        {
            tcgNames = new Dictionary<string, string>();
            DirectoryInfo di = new DirectoryInfo("".GetBaseDirectory() + "Configs\\");
            FileInfo[] files = di.GetFiles();
            foreach (FileInfo fi in files)
            {
                if (fi.Extension.Equals(".tcg"))
                {
                    tcgNames.Add(fi.Name, fi.Name);
                }
            }
        }

        private string _ConfigName;
        public string ConfigName
        {
            get { return _ConfigName; }
            set
            {
                _ConfigName = value;
                OnPropertyChanged("ConfigName");
            }
        }

        private string _Scan1IPAddress = "127.0.0.1";
        public string Scan1IPAddress
        {
            get { return _Scan1IPAddress; }
            set
            {
                _Scan1IPAddress = value;
                OnPropertyChanged("Scan1IPAddress");
            }
        }
        private int _Scan1PortNum;
        public int Scan1PortNum
        {
            get { return _Scan1PortNum; }
            set
            {
                _Scan1PortNum = value;
                OnPropertyChanged("Scan1PortNum");
            }
        }
        private string _Scan2IPAddress = "127.0.0.1";
        public string Scan2IPAddress
        {
            get { return _Scan2IPAddress; }
            set
            {
                _Scan2IPAddress = value;
                OnPropertyChanged("Scan2IPAddress");
            }
        }
        private int _Scan2PortNum;
        public int Scan2PortNum
        {
            get { return _Scan2PortNum; }
            set
            {
                _Scan2PortNum = value;
                OnPropertyChanged("Scan2PortNum");
            }
        }

        //private string _Scan1PortName;
        //public string Scan1PortName
        //{
        //    get { return _Scan1PortName; }
        //    set
        //    {
        //        _Scan1PortName = value;
        //        OnPropertyChanged("Scan1PortName");
        //    }
        //}
        //private string _Scan2PortName;
        //public string Scan2PortName
        //{
        //    get { return _Scan2PortName; }
        //    set
        //    {
        //        _Scan2PortName = value;
        //        OnPropertyChanged("Scan2PortName");
        //    }
        //}

        private Dictionary<string, string> _PortNames;
        /// <summary>
        /// PortNames
        /// </summary>
        [System.Xml.Serialization.XmlIgnore]
        public Dictionary<string, string> PortNames
        {
            get { return _PortNames; }
            set
            {
                _PortNames = value;
                OnPropertyChanged("PortNames");
            }
        }


        public void LoadPortNames()
        {
            var ports = new Dictionary<string, string>();

            string[] portNames = Bandit.Helpers.BanditSerialPort.GetPortNames();

            if (portNames != null && portNames.Length > 0)
            {

                foreach (var item in portNames)
                {
                    ports.Add(item.GetBetweenValue("(", ")"), item);
                }

            }
            ports.Add("Com999", "Com999");

            this.PortNames = ports;
        }

      
    }
}
